<?php
/**
* category class page
*
* 分类管理
* @author jason <phnessu4@gmail.com>
* @version 1.0
*/
define('UNIT_CMS','1');
define('UNIT_MODULE','2');
define('UNIT_PLUGIN','3');
define('UNIT_TEMPLATE','4');
define('UNIT_MENU','5');


class Category{

	public $data = array();
	public $id	 = '';
	
	/* 数据库参数 */
	protected $db;
	protected $db_table = 'category';
	
	/**
	* 单元常量(引用自全局define变量)
	* @var integer 
	*/
	private $unit = '';
	
	/**
	 * @param $unit 所属单元
	 * @return 
	 */
	public function __construct($unit){
		$this->unit = $unit;	
		$this->db = new DB();
		//获取数据库表名
		$this->db_table = $this->db->getTableName($this->db_table);
	}
	
	/**
	 * 添加分类
	 * @param $data	数据 
	 * @return 
	 */
	public function addCat($data){
		try{
			$sql = "INSERT INTO $this->db_table 
				(`c_name`, `c_description`, `c_unit`, `c_sid`, `c_pid`) 
				VALUES 
				('$data[name]','$data[description]','$this->unit','$data[c_sid]','$data[pid]')";
			//TODO: 单元测试,前台菜单
			debug($sql);
			$this->db->Insert($sql);
			return true;
		}catch (Exception $e){
			echo $e->getMessage();
		}
	}
	
	/**
	 * 删除分类
	 * @param $id	分类id 
	 * @return 
	 */
	public function deleteCat($id){
	if($this->hasChild($id)){
		return false;
	}else{
		try{
			$sql = "DELETE FROM $this->db_table WHERE `c_id` = '$id'";
			$this->db->delete($sql);
			return true;
		}catch (Exception $e){
			echo $e->getMessage();
		}
	}
	}
	
	/**
	 * 更新分类
	 * @param $id	分类id
	 * @param $data	数据 
	 * @return 
	 */
	public function updateCat($id,$data){
		try{
			$sql = "UPDATE $this->db_table 
				 SET `c_name` = '$data[c_name]',
				`c_description` = '$data[c_desription]',
				`c_unit` = '$data[c_unit]',
				`c_sid` = '$data[c_sid]',
				`c_pid` = '$data[c_pid]' WHERE `category`.`c_id` = $id";
			$this->db->update($sql);
			return true;
		}catch (Exception $e){
			echo $e->getMessage();
		}
	}
	
	/**
	 * 查询所有分类,或单独分类
	 * @param ($id)	分类id (可选参数)
	 * @return 
	 */
	public function selectCat($id){
		try{
			if(empty($id)){
				$sql = "SELECT * FROM $this->db_table WHERE `c_unit` = '$this->unit'";
			}else{
				$sql = "SELECT * FROM $this->db_table WHERE `c_id` = '$id' and `c_unit` = '$this->unit'";
			}
			$r = $this->db->select($sql);
			return $r;
		}catch (Exception $e){
			echo $e->getMessage();
		}
	}
	
	/**
	 * 查询父分类
	 * @return 
	 */
	public function selectParentCat(){
		try{
			$sql = "SELECT * FROM $this->db_table WHERE `c_unit` = '$this->unit' AND `c_pid`= '0'";
			$r = $this->db->select($sql);
			return $r;
		}catch (Exception $e){
			echo $e->getMessage();
		}
	}
	
	/**
	 * 查询父类是否存在
	 * @return bool
	 */
	public function hasParent($id){
		try{
			$sql = "SELECT `c_pid` FROM $this->db_table WHERE `c_id`= '$id'";
			$r = $this->db->query($sql)->fetch(PDO::FETCH_ASSOC);
			if($r['cid']){
				return true;
			}else{
				return false;
			}
		}catch (Exception $e){
			echo $e->getMessage();
		}
	}
	/**
	 * 查询父类是否存在
	 * @return bool
	 */
	public function hasChild($id){
		try{
			$sql = "SELECT `c_id` FROM $this->db_table WHERE `c_pid`= '$id'";
			$r = $this->db->query($sql)->rowCount();
			if($r){
				return true;
			}else{
				return false;
			}
		}catch (Exception $e){
			echo $e->getMessage();
		}
	}
}
/**
 * 文章分类
 * 
 * 继承逻辑分类
 */
class CmsCat extends Category{
	public function __construct(){
		parent::__construct(UNIT_CMS);
	}
}

class ModuleCat extends Category{
	public function __construct(){
		parent::__construct(UNIT_MODULE);
	}
}

class PluginCat extends Category{
	public function __construct(){
		parent::__construct(UNIT_PLUGIN);
	}
}

class MenuCat extends Category{
	public function __construct(){
		parent::__construct(UNIT_MENU);
	}
}
?>